package com.atguigu.edu.controller;


import com.atguigu.edu.entity.chapter.ChapterVo;
import com.atguigu.edu.service.ChapterService;
import com.atguigu.edu.entity.Chapter;
import com.atguigu.utils.R;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author ld
 * @since 2022-08-16
 */
@RestController
@RequestMapping("/eduservice/chapter")
//@CrossOrigin
@Api(description = "章节信息管理")
public class ChapterController {
    @Resource
    private ChapterService chapterService;
    //根据课程id获取章节和小节信息
    @GetMapping("/getAllChapterVideo/{courseId}")
    public R getAllChapterVideo(@PathVariable String courseId){
        List<ChapterVo> list=chapterService.getAllChapterVideo(courseId);
        return R.ok().data("chapterVideoList",list);
    }
    //添加章节信息
    @PostMapping("/addChapter")
    public R addChapter(@RequestBody Chapter chapter){
        chapterService.save(chapter);
        return  R.ok();
    }
    //根据id查询章节信息
    @GetMapping("/getChapter/{chapterId}")
    public R getChapter(@PathVariable String chapterId){
        Chapter chapter = chapterService.getById(chapterId);
        return R.ok().data("chapter",chapter);
    }
    //修改章节信息
    @PostMapping("/updateChapter")
    public R updateChapter(@RequestBody Chapter chapter){
        chapterService.updateById(chapter);
        return R.ok();
    }
    //根据id删除章节信息
    @DeleteMapping("/deleteChapter/{chapterId}")
    public R deleteChapter(@PathVariable String chapterId){
        boolean flag=chapterService.deleteChapter(chapterId);
        if(flag){
            return R.ok();
        }else {
            return R.error();
        }
    }
}

